import logging
import os
from unittest import skipIf

import pytest
from parameterized import parameterized
from tests.testing_utils import (
    RUNNING_ON_CI,
    RUNNING_TEST_FOR_MASTER_ON_CI,
    RUN_BY_CANARY,
    SKIP_DOCKER_TESTS,
    SKIP_DOCKER_BUILD,
    SKIP_DOCKER_MESSAGE,
)
from tests.integration.buildcmd.build_integ_base import (
    BuildIntegJavaBase,
)


LOG = logging.getLogger(__name__)

# SAR tests require credentials. This is to skip running the test where credentials are not available.
SKIP_SAR_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY


@pytest.mark.java
class TestBuildCommand_Java(BuildIntegJavaBase):
    @parameterized.expand(
        [
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_GRADLEW_IN_CONTAINER_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_GRADLEW_IN_CONTAINER_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_GRADLEW_IN_CONTAINER_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
        ]
    )
    @skipIf(SKIP_DOCKER_TESTS or SKIP_DOCKER_BUILD, SKIP_DOCKER_MESSAGE)
    def test_building_java_in_container(
        self, runtime, runtime_version, code_path, expected_files, expected_dependencies
    ):
        self._test_with_building_java(
            runtime,
            os.path.join(code_path, runtime_version),
            expected_files,
            expected_dependencies,
            "use_container",
            self.test_data_path,
        )

    @parameterized.expand(
        [
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_GRADLEW_IN_CONTAINER_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            # skip these tests until build images for Java 25 are released
            # (
            #     "java25",
            #     "25",
            #     BuildIntegJavaBase.USING_GRADLE_PATH,
            #     BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
            #     BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            # ),
            # (
            #     "java25",
            #     "25",
            #     BuildIntegJavaBase.USING_GRADLEW_IN_CONTAINER_PATH,
            #     BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
            #     BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            # ),
            # (
            #     "java25",
            #     "25",
            #     BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
            #     BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
            #     BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            # ),
            # (
            #     "java25",
            #     "25",
            #     BuildIntegJavaBase.USING_MAVEN_PATH,
            #     BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
            #     BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            # ),
        ]
    )
    @skipIf(SKIP_DOCKER_TESTS or SKIP_DOCKER_BUILD, SKIP_DOCKER_MESSAGE)
    @pytest.mark.al2023
    def test_building_java_in_container_al2023(
        self, runtime, runtime_version, code_path, expected_files, expected_dependencies
    ):
        self._test_with_building_java(
            runtime,
            os.path.join(code_path, runtime_version),
            expected_files,
            expected_dependencies,
            "use_container",
            self.test_data_path,
        )

    @parameterized.expand(
        [
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_GRADLEW_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java8.al2",
                "8",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_GRADLEW_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java11",
                "11",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_GRADLEW_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java17",
                "17",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_GRADLEW_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java21",
                "21",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
            (
                "java25",
                "25",
                BuildIntegJavaBase.USING_GRADLE_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java25",
                "25",
                BuildIntegJavaBase.USING_GRADLEW_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java25",
                "25",
                BuildIntegJavaBase.USING_GRADLE_KOTLIN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_GRADLE,
                BuildIntegJavaBase.EXPECTED_GRADLE_DEPENDENCIES,
            ),
            (
                "java25",
                "25",
                BuildIntegJavaBase.USING_MAVEN_PATH,
                BuildIntegJavaBase.EXPECTED_FILES_PROJECT_MANIFEST_MAVEN,
                BuildIntegJavaBase.EXPECTED_MAVEN_DEPENDENCIES,
            ),
        ]
    )
    def test_building_java_in_process(self, runtime, runtime_version, code_path, expected_files, expected_dependencies):
        self._test_with_building_java(
            runtime,
            os.path.join(code_path, runtime_version),
            expected_files,
            expected_dependencies,
            False,
            self.test_data_path,
        )
